package org.extframework.ld.encryption;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import org.extframework.ld.content.XmlContent;

/**
 * @author : Dong.Li
 * @created : 2013-1-17
 * @version : 1.0
 * @description :
 */
public class Des {

	private static Key key;
	private static String DataBaseKey = "3649vip_web";

	static {
		DataBaseKey = XmlContent.getDatabaseKey();
	}

	public static void getKey(String strKey) {
		try {
			KeyGenerator _generator = KeyGenerator.getInstance("DES");
			_generator.init(new SecureRandom(strKey.getBytes()));
			key = _generator.generateKey();
			_generator = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getEncString(String strMing) {
		byte[] byteMi = (byte[]) null;
		byte[] byteMing = (byte[]) null;
		String strMi = "";
		BASE64Encoder base64en = new BASE64Encoder();
		try {
			byteMing = strMing.getBytes("UTF8");
			byteMi = getEncCode(byteMing);
			strMi = base64en.encode(byteMi);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			base64en = null;
			byteMing = (byte[]) null;
			byteMi = (byte[]) null;
		}
		return strMi;
	}

	public static String getDesString(String strMi) {
		BASE64Decoder base64De = new BASE64Decoder();
		byte[] byteMing = (byte[]) null;
		byte[] byteMi = (byte[]) null;
		String strMing = "";
		try {
			byteMi = base64De.decodeBuffer(strMi);
			byteMing = getDesCode(byteMi);
			strMing = new String(byteMing, "UTF8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			base64De = null;
			byteMing = (byte[]) null;
			byteMi = (byte[]) null;
		}
		return strMing;
	}

	private static byte[] getEncCode(byte[] byteS) {
		byte[] byteFina = (byte[]) null;
		Cipher cipher;
		try {
			cipher = Cipher.getInstance("DES");
			cipher.init(1, key);
			byteFina = cipher.doFinal(byteS);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cipher = null;
		}
		return byteFina;
	}

	private static byte[] getDesCode(byte[] byteD) {
		byte[] byteFina = (byte[]) null;
		Cipher cipher;
		try {
			cipher = Cipher.getInstance("DES");
			cipher.init(2, key);
			byteFina = cipher.doFinal(byteD);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cipher = null;
		}
		return byteFina;
	}

	public static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; ++n) {
			stmp = Integer.toHexString(b[n] & 0xFF);
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
			if (n >= b.length - 1)
				continue;
			hs = hs + ":";
		}
		return hs.toUpperCase();
	}

	public static String desDataBaseData(String str) {
		getKey(DataBaseKey);
		return getEncString(str);
	}

	public static String unDesDataBaseData(String str) {
		getKey(DataBaseKey);
		return getDesString(str);
	}

}
